package com.example.back.controller;


import com.example.back.model.SwitchGame;
import com.example.back.service.SwitchGameService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/switchGame")
public class SwitchGameController {
    private static final Logger logger = LogManager.getLogger(SwitchGameController.class);

    @Resource
    private SwitchGameService switchGameService;

    @RequestMapping("/getSwitchGameList")
    @ResponseBody
    public List<SwitchGame> getSwitchGameList(){
        List<SwitchGame> switchGameList = switchGameService.findAllSwitchGame();
//        for(int i=0; i<switchGameList.size(); i++){
//            logger.info(switchGameList.get(i).getYuyan());
//        }
        return switchGameList;
    }

    @RequestMapping("/insertSwitchGame")
    public String insertSwitchGame(String name, String introduction, String yuyan, String type, double score) {
        String rs = "";
        if (switchGameService.insertSwitchGame(name, introduction, yuyan, type, score)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新的Switch游戏添加成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>新的Switch游戏添加失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updateSwitchGame")
    public String updateSwitchGame(int id, String yuyan, double score) {
        String rs = "";
        if (yuyan != null && yuyan.length() != 0 && score != 0) {
            if (switchGameService.updateYuyanByID(id, yuyan) && switchGameService.updateScoreByID(id, score)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的语言版本和评分更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的语言版本和评分未完全更新！点击返回（3秒后自动返回）</button>";
            }
        } else if (yuyan != null && yuyan.length() != 0) {
            if (switchGameService.updateYuyanByID(id, yuyan)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的语言版本更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的语言版本更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else if (score != 0) {
            if (switchGameService.updateScoreByID(id, score)) {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的评分更新成功！点击返回（3秒后自动返回）</button>";
            } else {
                rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的评分更新失败！点击返回（3秒后自动返回）</button>";
            }
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>Switch游戏的语言版本和评分输入非法，请重新输入！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
    @RequestMapping("/deleteSwitchGame")
    public String deleteSwitchGame(int id) {
        String rs = "";
        if (switchGameService.deleteSwitchGame(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除Switch游戏成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除Switch游戏失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
    @RequestMapping("watchSPhoto")
    public String watchSPhoto(int id) {
        return "<img src='" + switchGameService.findPhotoByID(id) + "'>";
    }
}
